package com.soft.springhrms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soft.springhrms.mapper.EmployeesMapper;
import com.soft.springhrms.mapper.PositionsMapper;
import com.soft.springhrms.pojo.Departments;
import com.soft.springhrms.service.DepartmentsService;
import com.soft.springhrms.mapper.DepartmentsMapper;
import com.soft.springhrms.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DELL
 * @description 针对表【departments】的数据库操作Service实现
 * @createDate 2024-11-16 18:48:17
 */
@Service
public class DepartmentsServiceImpl extends ServiceImpl<DepartmentsMapper, Departments>
        implements DepartmentsService {
    @Autowired
    EmployeesMapper employeesMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Departments> getAllDepartments() {
        List<Departments> departmentsList=(List<Departments>) redisUtil.get("departmentsList");
        if(departmentsList==null ||departmentsList.isEmpty()){
            departmentsList= list();
        try {
            for (Departments department : departmentsList) {if (department!=null){if (department.getDepartmentManagerId()!=null)
                department.setManagerName(employeesMapper.selectById(department.getDepartmentManagerId()).getFullName());
                department.setDepartmentCount(employeesMapper.countByDepartment(department.getId()));}
            }
        }catch (Exception e){
            e.printStackTrace();
        }System.out.println("Redis中没有，从数据库获取");
            redisUtil.set("departmentsList", departmentsList,60*60);
        }
        return departmentsList;
    }
}




